﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.CheckCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.CheckCommandHandler
{
    public class GetMaterailsListByPurCodeCommandHandler : IRequestHandler<GetMaterailsListByPurCodeCommand, APIResult<List<OrderMaterials>>>
    {
        private readonly IBaseRepository<OrderMaterials> materialsBiz;
        private readonly IMapper mapper;

        public GetMaterailsListByPurCodeCommandHandler(IBaseRepository<OrderMaterials> materialsBiz, IMapper mapper)
        {
            this.materialsBiz = materialsBiz;
            this.mapper = mapper;
        }

        public Task<APIResult<List<OrderMaterials>>> Handle(GetMaterailsListByPurCodeCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<OrderMaterials>> result = new APIResult<List<OrderMaterials>>();
            var materialsList = materialsBiz.Quary(x => x.SaleCode == request.DocumentCode).Result;
            result.Count = materialsList.Count();
            result.Msg = APIEnum.成功.ToString();
            result.Code = APIEnum.成功;
            result.data = materialsList.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return Task.FromResult(result);
        }  
    }
}
